<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        function insert(queue,floorNum,current){
            console.log("start",queue);
            console.log("insert",floorNum);
            console.log("current",current);
            if(queue.length===0){
                queue.push(floorNum);
            }
            else if(queue[0]>current&&floorNum>current){
                if(queue[queue.length-1]<floorNum){
                    queue.push(floorNum);
                }
                else{
                    for(var i=0;i<queue.length;i++){
                        if(queue[i]>floorNum){
                            queue.splice(i,0,floorNum);
                            break;
                        }
                    }
                }
            }
            else if(queue[0]<current&&floorNum<current){
                if(queue[queue.length-1]>floorNum){
                    queue.push(floorNum);
                }
                else{
                    for(var i=0;i<queue.length;i++){
                        if(queue[i]<floorNum){
                            queue.splice(i,0,floorNum);
                            break;
                        }
                    }
                }
            }
            console.log("end",queue);
        }
        //insert([],3,0);
        // insert([1],3,0);
        // insert([1,5],3,0);
        //insert([5,3],4,6);
        // insert([6,4,1],2,8);
        // insert([4],2,6);


        function refreshDestination(elevator,current,pressed){
            console.log("refreshDestination",current,pressed);
            if(pressed.length===0){
                elevator.keepMoving="stopped";
                console.log("电梯停止运行");
            }
            else if(elevator.keepMoving==="up"){
                for(var i=0;i<pressed.length;i++){
                    if(pressed[i]>current){
                        elevator.destinationQueue=pressed.slice(i);
                        break;
                    }
                }
                elevator.keepMoving="down";
            }
            else{
                pressed.reverse();
                console.log("reverse",pressed);
                for(var i=0;i<pressed.length;i++){
                    if(pressed[i]<current){
                        elevator.destinationQueue=pressed.slice(i);
                        break;
                    }
                }
                elevator.keepMoving="up";
            }
            console.log("end",elevator.destinationQueue);
        }
        // refreshDestination({"keepMoving":"up"},1,[2, 3, 4]);
        // refreshDestination({"keepMoving":"down"},1,[2, 3, 4]);


        //电梯:重新规划停靠线路
        function replanDestination(floors,keepMoving,current,pressed){
            // var keepMoving=elevator.keepMoving;
            // if(keepMoving==="stopped"){
            //     autoRun(elevator);
            // }
            // var current=elevator.currentFloor();
            // var pressed=elevator.getPressedFloors();
            var pressedFloors=new Array(floors.length);
            var queue=[];
            for(var i=0;i<pressed.length;i++){
                pressedFloors[pressed[i]]=true;
            }
            if(keepMoving==="up"){
                var backFloor=-1;
                for(var i=current;i<floors.length;i++){
                    if(floors[i].wait["up"]===true||pressedFloors[i]===true){
                        queue.push(i);
                    }
                    if(floors[i].wait["down"]===true&&i>backFloor){
                        backFloor=i;
                    }
                }
                if((queue.length===0&&backFloor>-1)||backFloor>queue[queue.length-1]){
                    queue.push(backFloor);
                }
            }
            else{
                var backFloor=floors.length;
                for(var i=current;i>=0;i--){
                    if(floors[i].wait["down"]===true||pressedFloors[i]===true){
                        queue.push(i);
                    }
                    if(floors[i].wait["up"]===true&&i<backFloor){
                        backFloor=i;
                    }
                }
                if((queue.length===0&&backFloor<floors.length)||backFloor<queue[queue.length-1]){
                    queue.push(backFloor);
                }
            }
            return queue;
        }
        var queue=replanDestination([
            {"wait":{"up":false,"down":false}},
            {"wait":{"up":false,"down":true}},
            {"wait":{"up":false,"down":false}}],
            "up",0,[]
        );
        console.log(queue);

        var queue=replanDestination([
            {"wait":{"up":true,"down":false}},
            {"wait":{"up":false,"down":false}},
            {"wait":{"up":false,"down":false}}],
            "down",2,[]
        );
        console.log(queue);
    </script>
</body>
</html>